using System;
using System.Collections.Generic;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace TenantManagementService.Monitoring
{
    /// <summary>
    /// 监控指标值实体
    /// </summary>
    public class MonitoringMetricValue : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; protected set; }

        /// <summary>
        /// 监控指标ID
        /// </summary>
        public virtual Guid MetricId { get; protected set; }

        /// <summary>
        /// 指标值
        /// </summary>
        public virtual decimal Value { get; protected set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public virtual DateTime Timestamp { get; protected set; }

        /// <summary>
        /// 维度（JSON格式）
        /// </summary>
        public virtual string? Dimensions { get; protected set; }

        /// <summary>
        /// 是否为聚合值
        /// </summary>
        public virtual bool IsAggregated { get; protected set; } = false;

        /// <summary>
        /// 聚合窗口（分钟）
        /// </summary>
        public virtual int? AggregationWindow { get; protected set; }

        /// <summary>
        /// 聚合类型（avg, sum, min, max, count）
        /// </summary>
        public virtual string? AggregationType { get; protected set; }

        protected MonitoringMetricValue() { }

        public MonitoringMetricValue(
            Guid id,
            Guid metricId,
            decimal value,
            DateTime timestamp,
            Dictionary<string, string>? dimensions = null,
            Guid? tenantId = null,
            bool isAggregated = false,
            int? aggregationWindow = null,
            string? aggregationType = null) : base(id)
        {
            MetricId = metricId;
            Value = value;
            Timestamp = timestamp;
            Dimensions = dimensions != null && dimensions.Count > 0 ?
                System.Text.Json.JsonSerializer.Serialize(dimensions) : null;
            TenantId = tenantId;
            IsAggregated = isAggregated;
            AggregationWindow = aggregationWindow;
            AggregationType = aggregationType;
        }

        /// <summary>
        /// 更新值
        /// </summary>
        public virtual MonitoringMetricValue UpdateValue(decimal value)
        {
            Value = value;
            return this;
        }

        /// <summary>
        /// 获取维度字典
        /// </summary>
        public virtual Dictionary<string, string>? GetDimensions()
        {
            if (string.IsNullOrWhiteSpace(Dimensions))
            {
                return null;
            }

            try
            {
                return System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(Dimensions);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 设置维度
        /// </summary>
        public virtual MonitoringMetricValue SetDimensions(Dictionary<string, string>? dimensions)
        {
            Dimensions = dimensions != null && dimensions.Count > 0 ?
                System.Text.Json.JsonSerializer.Serialize(dimensions) : null;
            return this;
        }

        /// <summary>
        /// 标记为聚合值
        /// </summary>
        public virtual MonitoringMetricValue MarkAsAggregated(int window, string type)
        {
            IsAggregated = true;
            AggregationWindow = window;
            AggregationType = type;
            return this;
        }
    }
}